今天的進度是承上篇重構怪物的移動模式的第五個 level —— 怪物掉落,的實作內容
class Mob(pygame.sprite.Sprite):
def __init__(self, construction: dict, **kwargs):
self.is_attack = False
self.attack_ract = self.rect.copy()
self.attack_vel = Vec(random.randint(-10, 10), random.randint(5, 10))
is_attack
為判斷 mob 是否在攻擊狀態attack_rect
為 mob 攻擊的矩形attack_vel
為 mob 攻擊時的速度# before
class Mob(pygame.sprite.Sprite):
def act(self):
if self.id > 3 and self.used_frame - self.last_shoot_frame > self.shoot_cd:
self.shoot()
id
大於等於 5 level 且不在攻擊(掉落)狀態,隨機選擇(1/6)是否攻擊# after
class Mob(pygame.sprite.Sprite):
def act(self):
if self.id > 3 and not self.is_attack and self.used_frame - self.last_shoot_frame > self.shoot_cd:
self.shoot()
if self.id >= 5 and not self.is_attack:
self.is_attack = random.choice([True, False, False, False, False, False])
# before
class Mob(pygame.sprite.Sprite):
def get_obj_progress_data(self) -> dict or list:
progress_date_list.append(create_image_view_data(self.image_id, *self.rect.topleft, self.rect.width, self.rect.height, self.angle))
return progress_date_list
# after
class Mob(pygame.sprite.Sprite):
def get_obj_progress_data(self) -> dict or list:
if self.is_attack:
progress_date_list.append(create_image_view_data(self.image_id, *self.attack_ract.topleft, self.attack_ract.width, self.attack_ract.height, self.angle))
self.attack_ract.center += self.attack_vel
if not self.attack_ract.colliderect(self.play_rect_area):
self.attack_ract = self.rect.copy()
self.is_attack = False
else:
progress_date_list.append(create_image_view_data(self.image_id, *self.rect.topleft, self.rect.width, self.rect.height, self.angle))
return progress_date_list
今日檔案更新有: